# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname='firebird'
_build_sys=autotools
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=3.0.0.31894.22cc629
pkgrel=2
pkgdesc="Cross-platform relational database offering many ANSI SQL standard features (mingw-w64)"
url="https://www.firebirdsql.org/"
license=('custom')
arch=('any')
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-libfbclient")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-libfbclient")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=(
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-icu"
         $([[ "$_build_sys" == "autotools" ]] && echo \
           "${MINGW_PACKAGE_PREFIX}-libtommath" \
         )
         "${MINGW_PACKAGE_PREFIX}-zlib"
        )
makedepends=(#"${MINGW_PACKAGE_PREFIX}-btyacc" # Maybe add check in firebird presence of system btyacc
             $([[ "$_build_sys" == "cmake" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-cmake" \
             )
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libatomic_ops"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "git"
            )
options=('!debug' 'strip')
source=("${_realname}"::"git+https://github.com/Alexpux/firebird-git-svn.git"
        mingw-w64-trunk.patch)
sha256sums=('SKIP'
            'ccf5c85f487e2758f9d15e507e10fef238d3e262eef2ef328a73957d7b4000a4')


pkgver() {
  cd "${_realname}"
  source ${srcdir}/${_realname}/src/misc/writeBuildNum.sh
  printf '%s.%s.%s.%s.%s' ${MajorVer} ${MinorVer} ${RevNo} ${BuildNum} "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${_realname}
  git am ${srcdir}/mingw-w64-trunk.patch

  if [ "${_build_sys}" = "autotools" ]; then
    NOCONFIGURE=1 ./autogen.sh
  fi
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  
  if [ "${_build_sys}" = "autotools" ]; then
    cp -rf ${_realname} build-${MINGW_CHOST}
    cd build-${MINGW_CHOST}
    local _debug_conf=
    if check_option "debug" "y"; then
      _debug_conf="--enable-debug"
    fi
    
    ./configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --enable-static \
      --enable-shared \
      ${_debug_conf} \
      --disable-rpath \
      --with-fbbin=${MINGW_PREFIX}/bin \
      --with-fbsbin=${MINGW_PREFIX}/bin \
      --with-fbconf=${MINGW_PREFIX}/etc/${_realname} \
      --with-fblib=${MINGW_PREFIX}/lib \
      --with-fbinclude=${MINGW_PREFIX}/include \
      --with-fbdoc=${MINGW_PREFIX}/share/${_realname}/doc \
      --with-fbudf=${MINGW_PREFIX}/share/${_realname}/udf \
      --with-fbsample=${MINGW_PREFIX}/share/${_realname}/samples \
      --with-fbsample-db=${MINGW_PREFIX}/share/${_realname}/samples/empbuild \
      --with-fbhelp=${MINGW_PREFIX}/share/${_realname}/help \
      --with-fbintl=${MINGW_PREFIX}/share/${_realname}/intl \
      --with-fbmisc=${MINGW_PREFIX}/share/${_realname}/misc \
      --with-fbsecure-db=${MINGW_PREFIX}/etc/${_realname} \
      --with-fbmsg=${MINGW_PREFIX}/etc/${_realname} \
      --with-fblog=${MINGW_PREFIX}/var \
      --with-fbglock=${MINGW_PREFIX}/var \
      --with-fbplugins=${MINGW_PREFIX}/share/${_realname}

    CC=${MINGW_PREFIX}/bin/gcc \
    make -j1
    
  elif [ "${_build_sys}" = "cmake" ]; then
    mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
    
    local _build_conf="Release"
    if check_option "debug" "y"; then
      _build_conf="Debug"
    fi

    ${MINGW_PREFIX}/bin/cmake.exe \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${pkgdir}${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_build_conf} \
      ../${_realname}

    make -j1
  fi
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}

  if [ "${_build_sys}" = "autotools" ]; then
    local _dir=Release
    if check_option "debug" "y"; then
      _dir=Debug
    fi
    
    mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,etc/${_realname},include,lib,share/${_realname}/{doc,help,intl,misc,plugins,samples,udf},var}
    cp -f gen/${_dir}/firebird/bin/*       ${pkgdir}${MINGW_PREFIX}/bin/
    cp -f gen/${_dir}/firebird/lib/*       ${pkgdir}${MINGW_PREFIX}/lib/
    cp -rf gen/${_dir}/firebird/include/*   ${pkgdir}${MINGW_PREFIX}/include/
    cp -f gen/${_dir}/firebird/UDF/*       ${pkgdir}${MINGW_PREFIX}/share/${_realname}/udf/
    cp -rf gen/${_dir}/firebird/examples/* ${pkgdir}${MINGW_PREFIX}/share/${_realname}/samples/
    cp -f gen/${_dir}/firebird/help/*      ${pkgdir}${MINGW_PREFIX}/share/${_realname}/help/
    cp -f gen/${_dir}/firebird/intl/*      ${pkgdir}${MINGW_PREFIX}/share/${_realname}/intl/
    cp -rf gen/${_dir}/firebird/plugins/*   ${pkgdir}${MINGW_PREFIX}/share/${_realname}/plugins/
    #cp -f gen/${_dir}/firebird/misc/*      ${pkgdir}${MINGW_PREFIX}/share/${_realname}/misc/
    
    cp -f gen/install/*.bat       ${pkgdir}${MINGW_PREFIX}/bin/
    cp -f gen/${_dir}/firebird/*.conf      ${pkgdir}${MINGW_PREFIX}/etc/${_realname}/
    cp -f gen/${_dir}/firebird/*.msg       ${pkgdir}${MINGW_PREFIX}/etc/${_realname}/
    cp -f gen/${_dir}/firebird/*.fdb       ${pkgdir}${MINGW_PREFIX}/etc/${_realname}/
    
    cp -rf doc/* ${pkgdir}${MINGW_PREFIX}/share/${_realname}/doc/
    
    touch ${pkgdir}${MINGW_PREFIX}/var/${_realname}.log
    
    # Remove build tools
    rm -f ${pkgdir}${MINGW_PREFIX}/bin/{brltable,codes,create_db,*build*,*boot*,*static*,*current*}.exe
  elif [ "${_build_sys}" = "cmake" ]; then
    echo "Empty"
  fi
}
